// states for the launcher
private enum LauncherState{ Inactive1, Active1 }

var currentLState : LauncherState = LauncherState.Active1;

var projectile : Rigidbody;   //object that we are spawning
//var position : Vector3;   //its position in the world
var rotation : Quaternion;  //its starting rotation
var forceDirection : Vector3;   //the force to add to it after spawning it
/*
Different Force Modes
Right now only Impulse and VelocityChange have any real
effect as Force and Acceleration require constant update
on the object. This would be handled by the object but 
the lauch script can check the mode and set the proper 
start for the object.

Force	 Add a continuous force to the rigidbody, using its mass.
Acceleration	 Add a continuous acceleration to the rigidbody, ignoring its mass.
Impulse	 Add an instant force impulse to the rigidbody, using its mass.
VelocityChange	 Add an instant velocity change to the rigidbody, ignoring its mass.
*/
var forceMode : ForceMode = ForceMode.Impulse;    //type of force to add Default = ForceMode.Impulse;

var launched : boolean = false;
var launchSound : AudioClip;

//category for selecting the time options
class SpawnTimeOptions{

  //minutes. second, and fraction (of a second) to wait before spawning a new object
  var minutes : int;  
  var seconds : int;
  var fraction : int;
  
  var minDelay : int = 0;
  var secDelay : int = 0;
  var fraDelay : int = 0;
  
  //used for random spawn time
  /*
  * To user random spawn times:
  * Each of the minutes, seconds, and fractions can be used 
  * as random separetely. Such as if we want to spawn something
  * every one minutes, but vary the seconds. Check the randomSeconds
  * checkbox and set the mintes to 1.
  * To have no mininum set Min to 0
  */
  var minutesRandom : boolean = false;
  var secondsRandom : boolean = false;
  var fractionRandom : boolean = false;
  
  var minutesMin : int = 0;
  var secondsMin : int = 0;
  var fractionMin : int = 0;
  
  var minutesMax : int = 1;
  var secondsMax : int = 1;
  var fractionMax : int = 1;
}

//intace of SpawnTimeOptions class
var SpawnTimeSelect : SpawnTimeOptions = SpawnTimeOptions();

private var startTime;
private var newRandMin = true;
private var newRandSec = true;
private var newRandFra = true;

private var delayOccured = false;


function Awake() {

   startTime = Time.time;
   
     if(!delayOccured==true && 0 == SpawnTimeSelect.minDelay && 0 == SpawnTimeSelect.secDelay && 0 == SpawnTimeSelect.fraDelay)
    delayOccured = true;

}

function FixedUpdate () {
  switch (currentLState) {
    case LauncherState.Active1:
        //Active();
      break;
    
    case LauncherState.Inactive1:
      break;
    
    default:
      Debug.Log("Something went wrong, Incorrect Launcher State");
      break;
  }
}

function Active(){
  //check if a new random minutes needs to to be generated
  if(newRandMin && SpawnTimeSelect.minutesRandom){
    SpawnTimeSelect.minutes = SpawnTimeSelect.minutesMin + Random.value * SpawnTimeSelect.minutesMax;
    newRandMin = false;
  }
  
  //check if a new random second needs to to be generated
  if(newRandSec && SpawnTimeSelect.secondsRandom){
    SpawnTimeSelect.seconds = SpawnTimeSelect.secondsMin + Random.value * SpawnTimeSelect.secondsMax;
    newRandSec = false;
  }
  
  //check if a new random fraction needs to to be generated
  if(newRandFra && SpawnTimeSelect.fractionRandom){
    SpawnTimeSelect.fraction = SpawnTimeSelect.fractionMin + Random.value * SpawnTimeSelect.fractionMax;
    newRandFra = false;
  }
  
  var guiTime = Time.time - startTime;
  
  //break down the time into minutes, seconds, and fractions
  var currentMin = guiTime / 60; 
  var currentSec = guiTime % 60;
  var currentFra = (guiTime * 100) % 100;
  
  if(!delayOccured==true && currentMin >= SpawnTimeSelect.minDelay && currentSec >=SpawnTimeSelect.secDelay && currentFra>= SpawnTimeSelect.fraDelay){
    delayOccured = true;
    //reset the timer
    startTime = Time.time; 
    guiTime = 0;
  }
    
  //check if it is time to spawn a new  object
  if( currentMin >= SpawnTimeSelect.minutes && currentSec >= SpawnTimeSelect.seconds && currentFra >= SpawnTimeSelect.fraction){
    launch();
    
    //reset the timer
    startTime = Time.time; 
    guiTime = 0;
    
    //set booleans to generate a new random time
    if(SpawnTimeSelect.minutesRandom)
      newRandMin = true;
      
    if(SpawnTimeSelect.secondsRandom)
      newRandSec = true;
      
    if(SpawnTimeSelect.fractionRandom)
      newRandFra = true;
      
    launched = true;
    //Debug.Log("set launched to be true!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    setState(LauncherState.Inactive1);
  }
}

function setOptions(state : LauncherState, fd : Vector3, m : int, s :int, f : int){
  //Debug.Log("set the option");
  forceDirection = fd;
  SpawnTimeSelect.minutes = m;
  SpawnTimeSelect.seconds = s;
  SpawnTimeSelect.fraction = f;
  setState(state);
}

function setState(state : LauncherState){
//Debug.Log("set the state to"+state);
  if(state == LauncherState.Active1 && currentLState == LauncherState.Inactive1)
    //launched = false;
  
  currentLState = state;
}

function getState(){
  return currentLState;
}

function launch(){
  var instance : Rigidbody = Instantiate(projectile, gameObject.transform.position, rotation);
    instance.AddForce(forceDirection, forceMode);
    setState(LauncherState.Inactive1);
	//audio.PlayOneShot(launchSound);
}
    
    

/* for debug reasons 
Ill keep this here for now just in case we want to see
the time...

var time = 0.0;
function OnGUI () {

  var guiTime = Time.time;// - startTime;
  var minute = guiTime / 60;
  var second = guiTime % 60;
  var fraction1 = (guiTime * 100) % 100;
  textTime = String.Format ("{0:00}:{1:00}:{2:000}", minute, second, fraction1); 
   
  GUI.Box (Rect (20,15,140,50), "Current Time");
   
  GUI.Label (Rect (25, 35, 100, 30), textTime); //changed variable name to textTime -->text is not a good variable name since it has other use already

}
/*
*/